#!/bin/sh

####################
##### Remove the Apache configuration
####################

#####
##### Variables
#####
aVers=`httpd -v | grep -c 'Apache/2.2'`
apath=/usr/sbin

##### OS X Leopard
if [ ${aVers} = 1 ]; then
	apacheVers="2.2"
	apachePath="/private/etc/apache2"
	apacheConf="${apachePath}/httpd.conf"
	apacheMods="/usr/libexec/apache2"
	lassoFolder="apache2.2"
	connector="Lasso8ConnectorforApache2.2.so"

##### OS X Tiger
else
	apacheVers="1.3"
	apachePath="/private/etc/httpd"
	apacheConf="${apachePath}/httpd.conf"
	apacheMods="/usr/libexec/httpd"
	lassoFolder="apache"
	connector="Lasso8ConnectorforApache.so"
fi


##########
##### Recreate folder.
##########
if [ ! -r "${apachePath}/users" ]; then
	echo "Recreating the ${apachePath}/users folder."
	mkdir -p "${apachePath}/users"
	chmod 755 "${apachePath}/users"
	chown root:wheel "${apachePath}/users"
fi

##########
##### Remove Lasso files.
##########

##### Remove Connector file.
if [ -f "${apacheMods}/${connector}" ]; then
	rm -f "${apacheMods}/${connector}"
fi

##### Move lasso8.conf
if [ -f "${apachePath}/users/lasso8.conf" ]; then
	mv -f "${apachePath}/users/lasso8.conf" "${apachePath}/"
fi


##########
##### Check if file exists
##########
if [ -r "${apacheConf}" ] ; then

	##### Check for text added by Lasso installer and remove
	if ( grep -q -e "^#Begin: Added by Lasso" "${apacheConf}" )
	then
		while [ $? = 0 ];
		do
			ed - "${apacheConf}" > /dev/null << 'LASSOREMOVE'
/#Begin: Added by Lasso [0-9]/,/#End: Added by Lasso [0-9]/d
w
q
LASSOREMOVE
		done

	##### Nothing to remove
	else
		echo "No changes made to httpd.conf file."
	fi

	##### Check if Apache starts and restart
	if ( "${apath}/apachectl" configtest 2> /dev/null )
	then
		"${apath}/apachectl" restart
	
	##### On failure, print failure
	else
		echo "Apache config test did not work...not starting Apache:"
		"${apath}/apachectl" configtest
	fi

	echo ""

##### No config file found.
else
	echo "Nothing to do, no httpd.conf file found."
fi
